/**
 * Copyright (C) 2011 Whisper Systems
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.appspot.eusms;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.appspot.eusms.crypto.InvalidPassphraseException;
import com.appspot.eusms.crypto.MasterSecretUtil;
import com.appspot.eusms.util.MemoryCleaner;

import org.whispersystems.textsecure.crypto.MasterSecret;

/**
 * Activity that prompts for a users's passphrase.
 *
 * @author Moxie Marlinspike
 */
public class PassphrasePromptActivity extends PassphraseActivity {

    private EditText passphraseText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.prompt_passphrase_activity);
        check_google_play();
        initializeResources();
    }


    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuInflater inflater = this.getSupportMenuInflater();
        menu.clear();

        inflater.inflate(R.menu.log_submit, menu);
        super.onPrepareOptionsMenu(menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
            case R.id.menu_submit_debug_logs:
                handleLogSubmit();
                return true;
        }

        return false;
    }

    private void handleLogSubmit() {
        Intent intent = new Intent(this, LogSubmitActivity.class);
        startActivity(intent);
    }

    private void check_google_play() {
//      Setup the google public key
        String help_time = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt";
        String help_day = "+siKEFDzE";
        String help_workday = "HIm7mmRTVseuqy/bIXVu8HHrkAPrIRSAkC5qBOKxf72XAQt69U/uT0H4UKlq9dkFBTVvDVgPZl6UOJfUH+X2Fm9IRG9BI8p1nDTEngb8NQt9d7z3lEPpMK0F1vbPQBBZfzhi8fNWTjtNmkUMhWZ201H4zeMK2GlJq8oOwA0QtLhpoRWc3Ta1OoE806Lt3mE/z8ZSegbcK/p4UpL4mv2iQlzOqpcVEYBcnx9I/fjdKnfdBmswxXsueJRBaKhNeRp7/fRy0fMdFCQ+CAQSxOe5RuJFtaz/adzJDiDQicYFAqIMjs3ZLsMVsf7pv5+urxkoSqBD6zakYUEwIDAQAB";
        //     String base64EncodedPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt+siKEFDzEHIm7mmRTVseuqy/bIXVu8HHrkAPrIRSAkC5qBOKxf72XAQt69U/uT0H4UKlq9dkFBTVvDVgPZl6UOJfUH+X2Fm9IRG9BI8p1nDTEngb8NQt9d7z3lEPpMK0F1vbPQBBZfzhi8fNWTjtNmkUMhWZ201H4zeMK2GlJq8oOwA0QtLhpoRWc3Ta1OoE806Lt3mE/z8ZSegbcK/p4UpL4mv2iQlzOqpcVEYBcnx9I/fjdKnfdBmswxXsueJRBaKhNeRp7/fRy0fMdFCQ+CAQSxOe5RuJFtaz/adzJDiDQicYFAqIMjs3ZLsMVsf7pv5+urxkoSqBD6zakYUEwIDAQAB";
        String base64EncodedPublicKey = help_time + help_day + help_workday;


    }

    private void initializeResources() {
        Button okButton = (Button) findViewById(R.id.ok_button);
        passphraseText = (EditText) findViewById(R.id.passphrase_edit);

        okButton.setOnClickListener(new OkButtonClickListener());
    }

    private class OkButtonClickListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            try {
                Editable text = passphraseText.getText();
                String passphrase = (text == null ? "" : text.toString());
                MasterSecret masterSecret = MasterSecretUtil.getMasterSecret(PassphrasePromptActivity.this, passphrase);

                MemoryCleaner.clean(passphrase);
                setMasterSecret(masterSecret);
            } catch (InvalidPassphraseException ipe) {
                passphraseText.setText("");
                Toast.makeText(getApplicationContext(),
                        R.string.PassphrasePromptActivity_invalid_passphrase_exclamation,
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void cleanup() {
        this.passphraseText.setText("");
        System.gc();
    }
}
